package com.liuya.safe.policy.handler;

import com.liuya.common.SpringUtils;
import com.liuya.db.script.*;
import com.liuya.db.sql.xml.*;
import com.liuya.db.sql.xml.BusinessData;
import com.liuya.db.sql.xml.DefineVariable;
import com.liuya.db.sql.xml.ExprGroup;
import com.liuya.entitle.ScriptTestResult;
import com.liuya.safe.util.Constants;
import com.liuya.safe.model.SafeUser;
import com.liuya.safe.policy.service.BusinessDataManagerService;
import com.liuya.db.script.AbstractPolicy;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Component("businessDataPolicyDesignHandler")
public class BusinessDataPolicyDesignHandler extends AbstractPolicyDesignHandler implements Serializable{

//    @Autowired
//    @Qualifier("businessDataManagerService")
//    private BusinessDataManagerService manager;
    BusinessDataType xml;

    public BusinessDataPolicyDesignHandler() {
    }

//    public BusinessDataPolicyDesignHandler(BusinessDataManagerService manager, BusinessDataType xmlUc) {
 public BusinessDataPolicyDesignHandler(BusinessDataType xmlUc) {
//        this.manager = manager;
        this.xml = xmlUc;
    }

    public BusinessDataType getXml() {
        return this.xml;
    }

    public void setXml(BusinessDataType xml) {
        this.xml = xml;
    }


    public String getPolicyType() {
        return "businessData";
    }

    public String getManagePage() {
        return "busiDataMng/openTree.do";
    }

    public String getDesignPageTitle() {
        return "Design Business Data: " + xml.getName();
    }

    public String getRawPageTitle() {
        return "Edit Business Data manually: " + xml.getName();
    }

    public DefineVariable[] getVariables() {
        return xml.getDefineVariable();
    }

    public void deleteVariable(int id) {
        xml.removeDefineVariableAt(id);
    }

    public void addVariable(DefineVariable var) {
        xml.addDefineVariable(var);
    }

    public void updateVariable(int varIndex, DefineVariable var) {
        xml.setDefineVariable(varIndex, var);
    }

    public ExprGroup getExprGroup() {
        return xml.getExprGroup();
    }

    public boolean save(String id) {
        try{
//            manager.updateBusinessData(id, (BusinessData) xml);
            BusinessDataManagerService mm = (BusinessDataManagerService) SpringUtils.getBean("businessDataManagerService");
            mm.updateBusinessData(id, (BusinessData) xml);
            return true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void setDesignMode() {
        xml.setIsRawScript(false);
    }

    public void setRawMode() {
        xml.setIsRawScript(true);
    }

    public void setRawScript(String script) {
        xml.getRawScript().setContent(script);
    }

    public String getRawScript() {
        return xml.getRawScript().getContent();
    }

    public ScriptTestResult run(SafeUser user, Object businessData, Map context) {
        AbstractPolicy policy2 = getPolicy();
        com.liuya.db.script.BusinessData script = (com.liuya.db.script.BusinessData) policy2;

        if (context == null) {
            context = new HashMap();
        }
        context.put(Constants.BUSINESS_DATA, businessData);

        return script.test(user, context, getQueryManager());
    }

    public AbstractPolicy transferXml2Policy() {
        return ScriptFactory.getBusinessData(xml, getQueryManager());
    }
}
